<template>
  <div class="register">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名" prop="username" ref="name">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="ruleForm.password" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input type="password" v-model="ruleForm.checkPass" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="ruleForm.phone" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="success" @click="submitForm('ruleForm')">注册</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
  </div>
</template>

<script>
export default {
  name: 'register',
  data () {
    // 自定义验证表单元素的方法
    // var validatePass = (rule, value, callback) => {
    //   if (value === '') {
    //     callback(new Error('请输入密码'))
    //   } else {
    //     if (this.ruleForm.checkPass !== '') {
    //       this.$refs.ruleForm.validateField('checkPass')
    //     }
    //     callback()
    //   }
    // }

    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.ruleForm.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      activeName: 'second',
      ruleForm: {
        username: '',
        password: '',
        checkPass: '',
        phone: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 5, message: '长度在2-5个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 16, message: '密码必须是6-16位', trigger: 'blur' },
          { pattern: /^(?![A-Z]+$)(?![a-z]+$)(?!\d+$)(?![\W_]+$)\S/, message: '密码必须包含数字、字母和下划线任意两种', trigger: 'blur' }
        ],
        checkPass: [
          { required: true, message: '确认密码不能为空', trigger: 'blur' },
          { validator: validatePass2, trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          // 这个只能验证手机号
          {pattern: /^0{0,1}(13[0-9]|15[7-9]|153|156|18[7-9])[0-9]{8}$/, message: '请输入合法手机号', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    // submitForm(formName) {
    //   //通过ref定位到form表单
    //   this.$refs[formName].validate(valid => {
    //     if (valid) {
    //       this.$axios({
    //         method: 'post',
    //         url: '/user/register',
    //         params: {
    //           userName: this.ruleForm.username,
    //           password: this.ruleForm.password,
    //           telephone:this.ruleForm.phone,
    //         }
    //       })
    //         .then(res => {
    //           if (res.data.state === "200") {
    //             alert("注册成功")
    //             this.$router.replace({path: '/login'})
    //           }
    //           if (res.data.state === "0") {
    //             alert(res.data.msg)
    //           }
    //         })
    //     }else{
    //       alert(res.data.msg)
    //     }
    //   });
    // },
    resetForm (formName) {
      // 实现重置表单元素数据
      this.$refs[formName].resetFields()
    }
  }
}
</script>
<style lang="css" scoped>
.register {
  width: 100%;
  height: 100%;
  background-size: 100% 100%;
}
  .demo-ruleForm{
    width: 400px;
    height: 400px;
  }
</style>
